/*
Copyright 2020 Twitter, Inc.
SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
https://openapi-generator.tech
Do not edit the class manually.
*/
package com.miyin.twwwwww;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.google.gson.reflect.TypeToken;

import com.twitter.clientlib.JSON;
import com.twitter.clientlib.model.StreamingTweet;
import com.twitter.clientlib.model.SingleTweetLookupResponse;

public class TweetsStreamListenersExecutor {
    private final ITweetsQueue tweetsQueue;
    private final List<TweetsStreamListener> listeners = new ArrayList<>();
    private final InputStream stream;
    private volatile boolean isRunning = true;

    public TweetsStreamListenersExecutor(InputStream stream) {
        this.tweetsQueue = new LinkedListTweetsQueue();
        this.stream = stream;
    }

    public TweetsStreamListenersExecutor(ITweetsQueue tweetsQueue, InputStream stream) {
        this.tweetsQueue = tweetsQueue;
        this.stream = stream;
    }

    public void addListener(TweetsStreamListener toAdd) {
        listeners.add(toAdd);
    }

    public void executeListeners() {
        if (stream == null) {
            System.out.println("Error: stream is null.");
            return;
        } else if (this.tweetsQueue == null) {
            System.out.println("Error: tweetsQueue is null.");
            return;
        }

        TweetsQueuer tweetsQueuer = new TweetsQueuer();
        TweetsListenersExecutor tweetsListenersExecutor = new TweetsListenersExecutor();
        tweetsListenersExecutor.start();
        tweetsQueuer.start();
    }

    public synchronized void shutdown() {
        isRunning = false;
        System.out.println("TweetsStreamListenersExecutor is shutting down.");
    }

    private class TweetsListenersExecutor extends Thread {
        @Override
        public void run() {
            processTweets();
        }

        private void processTweets() {
            StreamingTweet streamingTweet;
            try {
                while (isRunning) {
                    streamingTweet = tweetsQueue.poll();
                    if (streamingTweet == null) {
                        Thread.sleep(100);
                        continue;
                    }
                    for (TweetsStreamListener listener : listeners) {
                        listener.actionOnTweetsStream(streamingTweet);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private class TweetsQueuer extends Thread {
        @Override
        public void run() {
            queueTweets();
        }

        public void queueTweets() {
            JSON json = new JSON();
            Type localVarReturnType = new TypeToken<SingleTweetLookupResponse>() {
            }.getType();

            String line = null;
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
                while (isRunning) {
                    line = reader.readLine();
                    if (line == null || line.isEmpty()) {
                        Thread.sleep(100);
                        continue;
                    }
                    try {
                        tweetsQueue.add(StreamingTweet.fromJson(line));
                    } catch (Exception interExcep) {
                        interExcep.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                shutdown();
            }
        }
    }
}

interface ITweetsQueue {
    StreamingTweet poll();

    void add(StreamingTweet streamingTweet);
}

class LinkedListTweetsQueue implements ITweetsQueue {
    private final Queue<StreamingTweet> tweetsQueue = new LinkedList<>();

    @Override
    public StreamingTweet poll() {
        return tweetsQueue.poll();
    }

    @Override
    public void add(StreamingTweet streamingTweet) {
        tweetsQueue.add(streamingTweet);
    }
}

